<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>老师信息</title>
    <style>
        #container {
            width: 80%;
            margin: 10px auto;
        }
        #main[v-cloak]{
            display: none;
        }
        .teacher {
            width: 100%;
            margin: 0 auto;
            padding: 10px 0;
            border-bottom: 1px dashed gray;
            overflow: auto;
        }
        .teacher div {
            float: left;
        }
        .photo {
            height: 120px;
            width: 160px;
            {#border-radius: 75px;#}
            overflow: hidden;
            margin-left: 10px;
        }
        .info {
            width: 75%;
            margin-left: 30px;
        }
        .info div {
            clear: both;
            margin: 5px 10px;
        }
        .info span {
            margin-right: 25px;
        }
        .info a {
            text-decoration: none;
            color: blue;
        }
        .user {
            float: right;
            margin: 10px 10px;
        }
        .user+h1 {
            clear: both;
        }
        #photo {
            border-radius: 32px 32px 32px 32px;
        }
    </style>
</head>
<body>

<div id="main" v-colak ="">
    <h1>{{ name }}学派人物信息</h1>
    <div id="container" v-for="teacher in teachers">

        <hr>
        <div class="teacher" >
            <div class="photo">
                <img :src="'/static/'+ teacher.photo" width="140">
            </div>
            <div class="info">
                <div>
                    <span><strong>姓名：{{teacher.name}}</strong></span>
                    <span style="color: #ffbd09"><strong>💥身份：{{ teacher.hero }}</strong></span>
                    <span><strong>性别:{{ teacher.gender }}</strong></span>
                </div>
                <div class="intro">
                    {{ teacher.intro }}
                </div>
                <div class="comment">
                    <a href="" @click.prevent="praise(teacher)" >👍好评</a>&nbsp;&nbsp;(<strong>{{ teacher.good_count }}</strong>)
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="" @click.prevent="critical(teacher)">👎差评</a>&nbsp;&nbsp;(<strong>{{ teacher.bad_count }}</strong>)
                </div>
            </div>
        </div>

        </div>
        <div>
        <a href="" @click.prevent = "prev">上一页</a>
        <a href="" @click.prevent = "next">下一页</a>
        </div>


</div>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.11/vue.min.js"></script>
    <script>
        let app = new Vue({
            el:'#main',
            data:{
                teachers:[],
                name:[],
                nextPage:'',
                prevPage:'',

            },
            created(){
                fetch('/polls/teacher/'+location.search).then(resp => resp.json()).then(
                    json=>{
                        console.log(this)
                        this.teachers = json.results
                        this.name = json.results[0].subject
                        this.prevPage = json.previous
                        this.nextPage = json.next

                        console.log(this.teachers)
                    }
                )

            },
            methods:{

                prev(){
                    fetch(this.prevPage).then(resp =>resp.json())
                    .then(json=> {
                         this.teachers = json.results
                        this.name = json.results[0].subject
                        this.prevPage = json.previous
                        this.nextPage = json.next
                    })

                },

                next(){
                    fetch(this.nextPage).then(resp =>resp.json())
                    .then(json=> {
                         this.teachers = json.results
                        this.name = json.results[0].subject
                        this.prevPage = json.previous
                        this.nextPage = json.next
                    })
                },


                praise(teacher){
                    fetch('/polls/praise/?tno=' + teacher.no)
                    .then(resp => resp.json())
                    .then(json =>{
                        if (json.code == 200){
                            teacher.good_count = json.count
                        }else {
                            alert(json.msg)
                        }
                    })

                },
                critical(teacher){
                    fetch('/polls/badReview/?tno=' + teacher.no)
                    .then(resp => resp.json())
                    .then(json =>{
                        if (json.code == 200){
                            teacher.bad_count = json.count
                        }else {
                            alert(json.msg)
                        }
                    })
                },
            },
        })


    </script>


</body>
</html>